#!/bin/bash

NDIS_PATH=$1
: ${NDIS_PATH:=./}
echo $NDIS_PATH

SYSTEM_BIT=$($(which getconf) LONG_BIT)
SYS_KERN_VER=`uname -r`

if [ -f /etc/issue ]
then
	if [ "$(cat /etc/issue |grep Ubuntu)" != "" -a "$(cat /etc/issue |grep 8.10)" != "" ]
	then
		echo "The current system is Ubuntu 8.10"
		if [ $SYSTEM_BIT -gt 32 ]
		then
			TARGET_FILE="$NDIS_PATH"/Ubuntu810_64/hw_cdc_driver.ko
		else
			TARGET_FILE="$NDIS_PATH"/Ubuntu810/hw_cdc_driver.ko
		fi
	elif [ "$(cat /etc/issue |grep Ubuntu)" != "" -a "$(cat /etc/issue |grep 9.04)" != "" ]
	then
		echo "The current system is Ubuntu 9.04"
		if [ $SYSTEM_BIT -gt 32 ]
		then
			TARGET_FILE="$NDIS_PATH"/Ubuntu904_64/hw_cdc_driver.ko
		else
			TARGET_FILE="$NDIS_PATH"/Ubuntu904/hw_cdc_driver.ko	
		fi
	elif [ "$(cat /etc/issue |grep openSUSE)" != "" -a "$(cat /etc/issue |grep 11.0)" != "" ]
	then
		echo "The current system is openSUSE 11.0"
		if [ $SYSTEM_BIT -gt 32 ]
		then
			TARGET_FILE="$NDIS_PATH"/OpenSUSE11_64/hw_cdc_driver.ko
		else
			TARGET_FILE="$NDIS_PATH"/OpenSUSE11/hw_cdc_driver.ko
		fi
	elif [ "$(cat /etc/issue |grep openSUSE)" != "" -a "$(cat /etc/issue |grep 11.1)" != "" ]
	then
		echo "The current system is openSUSE 11.1"
		if [ $SYSTEM_BIT -gt 32 ]
		then
			TARGET_FILE="$NDIS_PATH"/OpenSUSE111_64/hw_cdc_driver.ko 
		else
			TARGET_FILE="$NDIS_PATH"/OpenSUSE111/hw_cdc_driver.ko  
		fi
	elif [ "$(cat /etc/issue |grep Fedora)" != "" -a "$(cat /etc/issue |grep 11)" != "" ]
	then
		echo "The current system is Fedora 11"
		if [ $SYSTEM_BIT -gt 32 ]
		then
			TARGET_FILE="$NDIS_PATH"/Fedora11_64/hw_cdc_driver.ko
		else
			TARGET_FILE="$NDIS_PATH"/Fedora11/hw_cdc_driver.ko
		fi
	elif [ "$(cat /etc/issue |grep Fedora)" != "" -a "$(cat /etc/issue |grep 10)" != "" ]
	then
		echo "The current system is Fedora 10"
		if [ $SYSTEM_BIT -gt 32 ]
		then
			TARGET_FILE="$NDIS_PATH"/Fedora10_64/hw_cdc_driver.ko
		else
			TARGET_FILE="$NDIS_PATH"/Fedora10/hw_cdc_driver.ko
		fi
		
	elif [ "$(cat /etc/issue |grep Fedora)" != "" -a "$(cat /etc/issue |grep 9)" != "" ]
	then
		echo "The current system is Fedora 9"
		if [ $SYSTEM_BIT -gt 32 ]
		then
			TARGET_FILE="$NDIS_PATH"/Fedora9_64/hw_cdc_driver.ko
		else
			TARGET_FILE="$NDIS_PATH"/Fedora9/hw_cdc_driver.ko
		fi
	else
		echo "The current system can not support ndis feature"
		exit 0
	fi

	TARGET_VER=`modinfo $TARGET_FILE |grep vermagic | awk '{print $2}'`
	if [ "$SYS_KERN_VER" = "$TARGET_VER" ]
	then
		cp $TARGET_FILE /lib/modules/$(uname -r)/kernel/drivers/net/usb/
		chmod a+x /lib/modules/$(uname -r)/kernel/drivers/net/usb/hw_cdc_driver.ko
		$(which depmod) -a
		$(which modprobe) hw_cdc_driver
	else
		CUR_PATH=`pwd`
		cd "$NDIS_PATH"/ndis_src
		./make_driver.sh
		cd "$CUR_PATH"
		if [ -f /lib/modules/$(uname -r)/kernel/drivers/net/usb/hw_cdc_driver.ko ]
		then
			echo -e "\033[34;1m\nThe Linux NDIS driver is installed successfully.\033[0;0m"
		else
			echo -e "\033[31;1m\nThere is some problems happened while compiling the ndis driver, \nplease enter any key to stop this compilation, \ncorrect the problems and compile again.\nOtherwise, NDIS feature can not be used.\033[0;0m"
			read -n 1
			exit 0

		fi
	fi	
fi
